all files / tabs/ tabs.vue

9.52% Statements 2/21
0% Branches 0/12
0% Functions 0/7
5.56% Lines 1/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                                                                                                                     
//
//
//
//
//
//
//
 
export default {
  name: 'mu-tabs',
  props: {
    value: {}
  },
  data () {
    return {
      tabLightStyle: {
        width: '100%',
        transform: 'translate3d(0, 0, 0)'
      }
    }
  },
  methods: {
    handleTabClick (value, tab) {
      if (this.value !== value) {
        this.$emit('change', value)
      }
    },
    getActiveIndex () {
      if (!this.$children || this.$children.length === 0) return -1
      let activeIndex = -1
      this.$children.forEach((tab, i) => {
        if (tab.value === this.value) {
          activeIndex = i
          return false
        }
      })
      return activeIndex
    },
    setTabLightStyle () {
      let x = this.getActiveIndex() * 100
      let len = this.$children.length
      this.tabLightStyle = {
        width: len > 0 ? (100 / len).toFixed(4) + '%' : '100%',
        transform: 'translate3d(' + x + '%, 0, 0)'
      }
    }
  },
  mounted () {
    this.setTabLightStyle()
  },
  watch: {
    value (val, oldVal) {
      if (val === oldVal) return
      this.setTabLightStyle()
    }
  }
}